package com.xmkj.winmoney.jifen;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.Entity.DuobaoDaShangBean;
import com.common.Entity.HomeEntity;
import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.DuoBaoDaShangJiFengListMethods;
import com.common.retrofit.method.DuoBaoJoinMethods;
import com.common.retrofit.method.JingPaiJoinMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.recyclerview.refresh.adapter.CommonAdapter;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.winmoney.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by leo
 * 创建日期：2018/5/22
 * 描述：
 */

public class UseMoneyJoinAcitivity extends BaseMvpActivity {
    public static final String JIFEN = "jifen";
    public static final String JINGPAIBEAN = "JINGPAIBEAN";
    @BindView(R.id.tv_see_more)
    TextView tvSeeMore;
    @BindView(R.id.tv_choose)
    ImageView tvChoose;
    @BindView(R.id.tv_jifen)
    TextView tvJifen;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    @BindView(R.id.rl_jingpai)
    RelativeLayout rlJingpai;
    @BindView(R.id.tv_support)
    TextView tvSupport;
    private String jifen;
    private DaShangAdapter adapter;
    List<DuobaoDaShangBean> bean = new ArrayList<>();
    private boolean hasChoose;
    private int currentPosition = -1;
    private HomeEntity.ListsEntity jinpaibean;
    private ArrayList<DuobaoDaShangBean> beans;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_use_money_join;
    }

    @Override
    protected void onViewCreated() {
        jifen = getIntent().getStringExtra(JIFEN);
        jinpaibean = (HomeEntity.ListsEntity) getIntent().getSerializableExtra(JINGPAIBEAN);
        if (EmptyUtils.isNotEmpty(jifen)) {
            rlJingpai.setVisibility(View.VISIBLE);
            recyclerview.setVisibility(View.GONE);
            tvJifen.setText("感谢你支持" + jifen + "金币");
        } else {
            rlJingpai.setVisibility(View.GONE);
            recyclerview.setVisibility(View.VISIBLE);
            if(EmptyUtils.isNotEmpty(jinpaibean)){
                gotoHttpRep();
                setRecyclerView();
            }
        }
    }

    @Override
    protected void doLogicFunc() {


        attachClickListener(tvChoose);
        attachClickListener(tvSupport);
        attachClickListener(tvSeeMore);
    }

    @Override
    protected void onViewClicked(View view) {
        if (tvChoose.getId() == view.getId()) {
            if (currentPosition < 0) {
                tvChoose.setBackgroundResource(R.mipmap.choose);
                tvSupport.setEnabled(true);
                currentPosition = 1;
            } else {
                tvChoose.setBackgroundResource(R.mipmap.circle);
                currentPosition = -1;
                tvSupport.setEnabled(false);
            }
        } else if (view.getId() == tvSupport.getId()) {  // 区分 todo  竞拍 还是 夺宝
            if (EmptyUtils.isNotEmpty(jinpaibean)&&EmptyUtils.isNotEmpty(jifen)) { // 竞拍
                    gotoJingPaiSupport();
            }else {
                gotoDuoBaoSupport();
            }
        } else if(view.getId()==tvSeeMore.getId()){
            gotoActivity(MianZeActivity.class);        }
    }
    private void gotoDuoBaoSupport() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.DUOBAOSUCEESS, true));
                dismissProgressDialog();
                showToastMsg("参与成功！");
                onBackPressed();


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        uid = SPUtils.getShareInt(Constans.UID);
        hashid =  SPUtils.getShareString(Constans.HASHID);
        DuoBaoJoinMethods.getInstance().buyLuckyNum(subscriber, uid, hashid,jinpaibean.gid+"",jinpaibean.fvid+"",beans.get(currentPosition).score+"");
        rxManager.add(subscriber);
    }
    private void gotoJingPaiSupport() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {

                dismissProgressDialog();
                RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.JINGPAISUCEESS, true));
                showToastMsg("参与成功！");
                onBackPressed();


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        uid = SPUtils.getShareInt(Constans.UID);
        hashid =  SPUtils.getShareString(Constans.HASHID);
        JingPaiJoinMethods.getInstance().buyAuction(subscriber, uid, hashid,jinpaibean.gid+"",jinpaibean.fvid+"",jinpaibean.vid+"");
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("");
    }

    private void setRecyclerView() {
//        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
       /* GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);*/
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(false);


//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setLoadMoreEnabled(true);
        adapter = new DaShangAdapter(context, bean);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(adapter);


        adapter.setOnItemChildClickListener(new CommonAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(CommonAdapter adapter, View view, int position) {
                DuobaoDaShangBean infobean = (DuobaoDaShangBean) adapter.mData.get(position);
                if (view.getId() == R.id.iv_choose) {
                    if (infobean.is_select) {  // 已经被选中了
                    /*    DaShangTestBean modifyBean = new DaShangTestBean();
                        modifyBean.is_select = false ;
                        bean.set(position,modifyBean);*/
                        for (int i = 0; i < bean.size(); i++) {

                            DuobaoDaShangBean testBean = bean.get(i);
                            testBean.is_select = false;
                            bean.set(i, testBean);
                        }
                        adapter.notifyDataSetChanged();
                        currentPosition = -1;

                        tvSupport.setEnabled(false);
                    } else {  // 没有被选中

                        currentPosition = position;
                        for (int i = 0; i < bean.size(); i++) {

                            DuobaoDaShangBean testBean = bean.get(i);
//                            testBean.is_select = false ;
//                            position==i? testBean.is_select = true:
                            if (position == i) {
                                testBean.is_select = true;
                            } else {
                                testBean.is_select = false;
                            }

                            bean.set(i, testBean);

                        }
                        tvSupport.setEnabled(true);
                        adapter.notifyDataSetChanged();

                    }
                }
            }
        });


        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttpRep();
            }
        });
    }


    private void gotoHttpRep() {
        CommonSubscriber<ArrayList<DuobaoDaShangBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                beans = (ArrayList<DuobaoDaShangBean>) o;

                List<DuobaoDaShangBean> beanList = beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        DuoBaoDaShangJiFengListMethods.getInstance().selectScore(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID),jinpaibean.gid+"" );
        rxManager.add(subscriber);
    }

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);

    }
}
